package org.cn.osiris.pet.service.impl;


import org.cn.osiris.pet.domain.PetType;
import org.cn.osiris.pet.mapper.PetTypeMapper;
import org.cn.osiris.pet.service.IPetTypeService;
import org.cn.osiris.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {

    @Autowired
    private PetTypeMapper petTypeMapper;

    @Override
    public List<PetType> queryTree() {
        //先查询到所有
        List<PetType> allPetTypes = petTypeMapper.findAll();
        HashMap<Long, PetType> allPetTypeMap = new HashMap<>();
        List<PetType> petTypeTree = new ArrayList<>();
        for (PetType petType : allPetTypes) {
            allPetTypeMap.put(petType.getId(),petType);
        }
        for (PetType temp: allPetTypes) {
            //如果没有查到父级id
            if (temp.getParentId()==null){
                petTypeTree.add(temp);
            }else {
               //如果查到父级id 将他放在map中
                PetType parent = allPetTypeMap.get(temp.getParentId());
                if (parent!= null){
                    parent.getChildren().add(temp);
                }
            }
        }
        System.out.println(petTypeTree);
        return petTypeTree;
    }


}
